﻿using System;
using System.Security.Cryptography.X509Certificates;
using CefSharp;


namespace web_frame
{
    class MyRequestHandler : IRequestHandler
    {
        public event ResourceLoadCompleteEvent ResponseData;

        bool IRequestHandler.GetAuthCredentials(IWebBrowser chromiumWebBrowser, IBrowser browser, string originUrl, bool isProxy, string host, int port, string realm, string scheme, IAuthCallback callback)
        {
            return true;
        }

        bool IRequestHandler.OnBeforeBrowse(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IRequest request, bool userGesture, bool isRedirect)
        {
            return false;
        }

        bool IRequestHandler.OnCertificateError(IWebBrowser chromiumWebBrowser, IBrowser browser, CefErrorCode errorCode, string requestUrl, ISslInfo sslInfo, IRequestCallback callback)
        {
            return true;
        }



        bool IRequestHandler.OnOpenUrlFromTab(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, string targetUrl, WindowOpenDisposition targetDisposition, bool userGesture)
        {
            return false;
        }

        void IRequestHandler.OnRenderProcessTerminated(IWebBrowser chromiumWebBrowser, IBrowser browser, CefTerminationStatus status)
        {
            
        }

        void IRequestHandler.OnRenderViewReady(IWebBrowser chromiumWebBrowser, IBrowser browser)
        {
            
        }

        bool IRequestHandler.OnSelectClientCertificate(IWebBrowser chromiumWebBrowser, IBrowser browser, bool isProxy, string host, int port, X509Certificate2Collection certificates, ISelectClientCertificateCallback callback)
        {
            return false;
        }

        void IRequestHandler.OnDocumentAvailableInMainFrame(IWebBrowser chromiumWebBrowser, IBrowser browser)
        {
            
        }

        IResourceRequestHandler IRequestHandler.GetResourceRequestHandler(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IRequest request, bool isNavigation, bool isDownload, string requestInitiator, ref bool disableDefaultHandling)
        {
            MyResourceHandler handler = new MyResourceHandler();
            handler.ResponseData += ResponseDataEvent;
            Console.WriteLine("****************Request_Start****************");
            Console.WriteLine(request.Method.ToString());
            Console.WriteLine(request.Url);
            if(request.Method == "POST")
           {
                var elements = request.PostData.Elements;
                foreach (var element in elements)
                {
                    Console.WriteLine(element.GetBody());
                }
            }
            Console.WriteLine("****************Request_End****************");
            return handler;
        }

        private void ResponseDataEvent(string url, string Charset, string MimeType, int StatusCode, string StatusText, byte[] data)
        {
            ResponseData?.Invoke(url, Charset, MimeType, StatusCode, StatusText, data);
        }
    }
}